
<?php
require_once(dirname(__FILE__).'/../include/config.inc.php');
require_once(dirname(__FILE__).'/../include/mysqli.class.php');

class aliPayNotify{
	public $db;
	public function __construct($db){
		$this->db=$db;
    }

    /*
     * 支付宝充值回调
     */
    public function rechargeNotify($params){
        // 处理支付成功后的逻辑业务
        $order_info = $this->db->GetOne("select * from dyspw_order where order_num=".$params['out_trade_no']);
        $time = time();
        $month_time = strtotime(date('Y-m-d ',strtotime('+1 month')));//获取一个月后的时间戳
        $year_time = strtotime(date('Y-m-d H:i:s',strtotime('+1 year')));//获取一年后的时间戳
        if($order_info['recharge_type'] == 'month'){
            $end_time = $month_time;
        }elseif ($order_info['recharge_type'] == 'year') {
            $end_time = $year_time;
        }
        $Up_sql = "Update dyspw_order set order_status = 1 where id = ".$order_info['id'];
        $mem_sql = "Update dyspw_member set is_vip = ".$order_info['is_vip'].", start_time = '".$time."', end_time = '".$end_time."' where id = ".$order_info['uid'];
        $this->db->ExecNoneQuery($Up_sql);
        $this->db->ExecNoneQuery($mem_sql);

    }

    /*
     * 支付宝团购订单支付回调
     */
    public function confirmOrderNotify($params){
        $goodsorder = $this->db->GetOne("select * from dyspw_goods_order where order_num=".$params['out_trade_no']);
        $Up_sql = "Update dyspw_goods_order set state = 1 where id = ".$goodsorder['id'];
        $this->db->ExecNoneQuery($Up_sql);

        //商品扣减库存
        if($goodsorder['waimai'] && $goodsorder['order_details']){
            //外卖商品库存更新
            $goodsInfo=json_decode($goodsorder['order_details'],true);
            foreach ($goodsInfo as $val){
                $sql='update dyspw_goods set stock = stock-'.$val['num'].' WHERE id ='.$val['id'];
                $this->db->ExecNoneQuery($sql);
            }
        }else{
            //团购商品库存更新
            $sql='update dyspw_goods set stock = stock-1 WHERE id ='.$goodsorder['id'];
            $this->db->ExecNoneQuery($sql);
        }

        require_once "../phpqrcode.php";
        //生成核销二维码流程
        define('UPLOAD_PATH', '../images/');
        define('UPLOAD_URL', '/xiaochengxu/images/qr_code/orders_hexiao/');
        //二维码流程

        //判断是否存在可用文件夹，若没有可用则新建
        $file_path = UPLOAD_PATH . 'qr_code/orders_hexiao';
        if(!is_dir($file_path)){
            mkdir($file_path,777,true);
        }

        //进入生成二维码流程
        $qr_code_path = UPLOAD_PATH . 'qr_code/orders_hexiao/';//二维码保存的路径信息
        $url = 'https://'.$_SERVER['HTTP_HOST']."/xiaochengxu/hexiao.php?order_sn=".$params['out_trade_no'];//定义二维码包含的文字信息内容
        $qr_code_file = $qr_code_path . $params['out_trade_no'] . ".png";
        $size = 5;
        \QRcode::png($url, $qr_code_file, QR_ECLEVEL_M, $size);//png方法生成二维码并保存至指定文件夹

        $qr_url = 'https://'.$_SERVER['HTTP_HOST'] . UPLOAD_URL . $params['out_trade_no'] . ".png";
        //二维码生成成功后，二维码地址入库操作
        $Up = "Update dyspw_goods_order set hexiao_qr = '".$qr_url."' where id = ".$goodsorder['id'];
        $this->db->ExecNoneQuery($Up);
    }

}

$params = $_POST;
$aliPayment=new \aliPayment($globalConfig['aliPay']);
$sign = $params['sign'];
$params['sign_type'] = null;
$params['sign'] = null;
$result = $aliPayment->verifySign($aliPayment->stringSign($params), $sign);
if($result){
    if ($params['trade_status'] == 'TRADE_FINISHED' || $params['trade_status'] == 'TRADE_SUCCESS') {
        $aliPayNotify=new aliPayNotify($dosql);
        switch ($params['passback_params']){
            case 'recharge':
                $aliPayNotify->rechargeNotify($params);
                break;
            case 'confirmOrder':
                $aliPayNotify->confirmOrderNotify($params);
                break;
            default :
                return 'fail';
                break;
        }
        return 'success';
    }else{
        return 'fail';
    }
}else{
    return 'fail';
}
?>
